package com.Beans.Sessions;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;



public class Session {
	
	
	public static void addSession(int id, String Nombre, String Apellido, String correo, String user, String cargo, int id_area){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(true);
		
		sesion.setAttribute("Id", id);
		sesion.setAttribute("Nombre", Nombre);
		sesion.setAttribute("Apellido", Apellido);
		sesion.setAttribute("Correo", correo);
		sesion.setAttribute("User", user);
		sesion.setAttribute("Cargo", cargo);
		sesion.setAttribute("IdArea", id_area);
	}
	
	public static void closeSession(){
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		((HttpSession) ctx.getSession(false)).invalidate();	
	}
	
	public static Integer getId(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		Integer Id = (Integer)sesion.getAttribute("Id");
		return Id;
	}
	
	public static String getNombre(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		String Nombre = (String)sesion.getAttribute("Nombre");
		return Nombre;
	}
	
	public static String getApellido(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		String Apellido = (String)sesion.getAttribute("Apellido");
		return Apellido;
	}
	
	public static String getCorreo(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		String Correo = (String)sesion.getAttribute("Correo");
		return Correo;
	}
	
	public static String getUser(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		String User = (String)sesion.getAttribute("User");
		return User;
	}
	
	public static String getCargo(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		String Cargo = (String)sesion.getAttribute("Cargo");
		return Cargo;
	}
	public static int getIdArea() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		int IdArea = (Integer) sesion.getAttribute("IdArea");
		return IdArea;
	}
	
	public static String getPagina() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
	    return ctx.getRequestPathInfo();
	}
	
	public static void redirectTo(String url) {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		String ctxPath = ((ServletContext) ctx.getContext()).getContextPath();
		try { 
			ctx.redirect(ctxPath + "/faces" + url); 
		}catch(IOException ex) { 
		
		}
	}

	public static void addErrorMessage(String msg) {
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
	    FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}
	
	public static FacesContext getFacesContext(){
		return FacesContext.getCurrentInstance();
	}
	/*public static void addObject(Object object){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(true);
		
		sesion.setAttribute("Object", object);
	}
	public static Object getObject() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession sesion = (HttpSession)context.getExternalContext().getSession(false);
		Object object = sesion.getAttribute("Object");
		return object;
	}*/
}
